package cn.b.sky.form.form.builder.impl;

import cn.b.sky.form.form.builder.BaseElementBuilder;
import cn.b.sky.form.model.BusinessField;
import cn.b.sky.form.model.ScreenField;
import cn.b.sky.form.utils.BusinessTypeDefinitionUtils;
import org.apache.commons.lang3.StringUtils;

public class ButtonBuilder extends BaseElementBuilder {

  @Override
  public String buildHtmlElement(ScreenField sf) {
    BusinessField field = sf.getBf();
    StringBuilder sb = new StringBuilder();
    sb.append("<input type=\"button\"");
    if (sf.getReadonlyFlag() == 1) {
      sb.append(" disabled=\"disabled\"");
    }
    if (!StringUtils.isBlank(field.getDbField()) && sf.getReadonlyFlag() != 1) {
      sb.append(" id=\"").append(field.getDbField()).append("\"");
      sb.append("name=\"").append(field.getDbField()).append("\"");;
    }
    //输入框检验
    String validation = this.getValidationAttribute(field);
    if (!StringUtils.isBlank(validation)) {
      sb.append(" class=\"").append(validation).append("\"");
    }
    sb.append(" value=\"").append(field.getFieldLabel()).append("\"");
    sb.append(" />");
    return sb.toString();
  }

  @Override
  public String toHtmlElement(ScreenField field) {
    StringBuilder fieldHtml = new StringBuilder();
    BusinessField bf = field.getBf();
    fieldHtml.append("<dl ");
    if(bf != null){
      String style = BusinessTypeDefinitionUtils.getFieldStyles(bf.getFieldType());
      if(!StringUtils.isBlank(style)){
        fieldHtml.append("class=\"").append(style).append("\"");
      }
    }
    fieldHtml.append(">");
    fieldHtml.append("<dt>");
    fieldHtml.append("&nbsp;");
    fieldHtml.append("</dt>");
    fieldHtml.append("<dd>");
    fieldHtml.append(buildHtmlElement(field));
    fieldHtml.append("</dd>");
    fieldHtml.append("</dl>");
    return fieldHtml.toString();
  }

}
